/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;


/**
 * This entity represents subject. Subject has specific code.
 * @author Tom
 * @version 1.0.0 beta
 */
@Entity
@Table(name = "subject")
@NamedQueries({
    @NamedQuery(name = "Subject.findByCode", query = "SELECT p FROM Subject p WHERE p.code = :code"),
    @NamedQuery(name = "Subject.all", query = "SELECT p FROM Subject p")
})
public class Subject implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(name = "code", nullable = false, unique = true)
    private String code;
    @Lob
    @Column(name = "description")
    private String description;
    @OneToMany(mappedBy="subject",cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @OrderBy("dateOfAssignment")
    private List<Assignment> mAssignments;
    @OneToMany(mappedBy = "subject", cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @OrderBy("code ASC")
    private List<GroupTaskPro> mGroups;

    public void setCode(String kod) {
        this.code = kod;
    }

    public void setmGroups(List<GroupTaskPro> mGroups) {
        this.mGroups = mGroups;
    }

    public void setDescription(String popisek) {
        this.description = popisek;
    }

    public String getCode() {
        return code;
    }

    /**
     * prida novou paralelku
     * @param p - nova paralelka
     */
    public void addGroup(GroupTaskPro p) {
        if (this.mGroups == null) {
            mGroups = new ArrayList<GroupTaskPro>();
        }
        mGroups.add(p);
        p.setSubject(this);
    }

    public List<GroupTaskPro> getmGroups() {
        return mGroups;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) id;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Subject)) {
            return false;
        }
        Subject other = (Subject) object;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "DL.entity.Subject[ id=" + id + " code=" + code + " ]";
    }

    public List<Assignment> getmAssignments() {
        return mAssignments;
    }

    public void setmAssignments(List<Assignment> mAssignments) {
        this.mAssignments = mAssignments;
    }

    public int getId() {
        return id;
    }
    
}
